/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package cn.zyx.sys.web;

import cn.zyx.common.GlobalAjaxJson;
import cn.zyx.common.entity.Page;
import cn.zyx.sys.entity.Post;
import cn.zyx.sys.service.PostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 岗位Controller
 * @author zyx
 */
@RestController
@RequestMapping(value = "/sys/post")
public class PostController {

	@Autowired
	private PostService postService;

	@ModelAttribute
	public Post get(@RequestParam(required=false) String id) {
		Post entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = postService.get(id);
		}
		if (entity == null){
			entity = new Post();
		}
		return entity;
	}

	/**
	 * 岗位列表数据
	 */
	//@RequiresPermissions("sys:post:list")
	@GetMapping("list")
	public GlobalAjaxJson list(Post post, HttpServletRequest request, HttpServletResponse response) {
		Page<Post> page = postService.findPage(new Page<Post>(request, response), post);
		return GlobalAjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取岗位数据
	 */
	//@RequiresPermissions(value={"sys:post:view","sys:post:add","sys:post:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public GlobalAjaxJson queryById(Post post) {
		return GlobalAjaxJson.success().put("post", post);
	}

	/**
	 * 保存岗位
	 */
	//@RequiresPermissions(value={"sys:post:add","sys:post:edit"},logical=Logical.OR)
	@PostMapping("save")
	public GlobalAjaxJson save(Post post, Model model) throws Exception{
		//新增或编辑表单保存
		postService.save(post);//保存
		return GlobalAjaxJson.success("保存岗位成功");
	}


	/**
	 * 批量删除岗位
	 */
	//@RequiresPermissions("sys:post:del")
	@DeleteMapping("delete")
	public GlobalAjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			postService.delete(postService.get(id));
		}
		return GlobalAjaxJson.success("删除岗位成功");
	}






}